package com.ctshk.rpc.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ctshk.common.dto.Result;
import com.ctshk.common.enums.IsDeletedCode;
import com.ctshk.common.enums.SystemError;
import com.ctshk.common.model.PageResponse;
import com.ctshk.common.model.TokenUser;
import com.ctshk.common.utils.EntityUtil;
import com.ctshk.rpc.system.dto.MainDataHousingTypeListDTO;
import com.ctshk.rpc.system.dto.MainDataHousingTypeQueryDTO;
import com.ctshk.rpc.system.entity.MainDataHousingType;
import com.ctshk.rpc.system.mapper.MainDataHousingTypeMapper;
import com.ctshk.rpc.system.req.MainDataHousingTypeAddReq;
import com.ctshk.rpc.system.req.MainDataHousingTypeListReq;
import com.ctshk.rpc.system.req.MainDataHousingTypeUpdateReq;
import com.ctshk.rpc.system.req.MainDataHousingTypeUpdateStatusReq;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ctshk.rpc.system.service.IMainDataHousingTypeService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 谢诗宏
 * @since 2021-02-02
 */
@DubboService
public class MainDataHousingTypeServiceImpl extends ServiceImpl<MainDataHousingTypeMapper, MainDataHousingType> implements IMainDataHousingTypeService {

    @Autowired
    private MainDataHousingTypeMapper mainDataHousingTypeMapper;

    /**
     * 新增住房类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result add(MainDataHousingTypeAddReq req, TokenUser tokenUser) {
        QueryWrapper<MainDataHousingType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_code",req.getTypeCode())
                .or()
                .eq("type_name",req.getTypeName());
        MainDataHousingType selectOne = mainDataHousingTypeMapper.selectOne(queryWrapper);
        if(null != selectOne){
            return Result.failed(SystemError.SYS_414);
        }
        MainDataHousingType mainDataHousingType = EntityUtil.copy(req, MainDataHousingType.class);
        long id = IdWorker.getId();
        mainDataHousingType.setId(id);
        LocalDateTime now = LocalDateTime.now();
        mainDataHousingType.setGmtCreate(now);
        mainDataHousingType.setGmtModified(now);
        mainDataHousingType.setIsDeleted(IsDeletedCode.NO.getCode());
        mainDataHousingType.setModifiedId(tokenUser.getId());
        mainDataHousingType.setModifiedName(tokenUser.getUsername());
        mainDataHousingType.setCreateId(tokenUser.getId());
        mainDataHousingTypeMapper.insert(mainDataHousingType);
        return Result.success(id);
    }

    /**
     * 编辑住房类型
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result update(MainDataHousingTypeUpdateReq req, TokenUser tokenUser) {
        QueryWrapper<MainDataHousingType> queryWrapper = new QueryWrapper<>();
        MainDataHousingType mainDataHousingType = EntityUtil.copy(req, MainDataHousingType.class);
        LocalDateTime now = LocalDateTime.now();
        mainDataHousingType.setGmtModified(now);
        mainDataHousingType.setModifiedId(tokenUser.getId());
        mainDataHousingType.setModifiedName(tokenUser.getUsername());
        mainDataHousingTypeMapper.updateById(mainDataHousingType);
        return Result.success(req.getId());

    }

    /**
     * 编辑住房类型状态
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Result updateStatus(MainDataHousingTypeUpdateStatusReq req,TokenUser tokenUser) {
        MainDataHousingType mainDataHousingType = EntityUtil.copy(req, MainDataHousingType.class);
        mainDataHousingType.setModifiedName(tokenUser.getUsername());
        mainDataHousingType.setModifiedId(tokenUser.getId());
        mainDataHousingType.setGmtModified(LocalDateTime.now());
        QueryWrapper<MainDataHousingType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",req.getId());
        queryWrapper.eq("is_deleted",IsDeletedCode.NO.getCode());
        MainDataHousingType selectOne = mainDataHousingTypeMapper.selectOne(queryWrapper);
        if(selectOne.getStatus() == 0){
            mainDataHousingType.setStatus(1);
        }else {
            mainDataHousingType.setStatus(0);
        }
        mainDataHousingTypeMapper.updateById(mainDataHousingType);
        return Result.success(req.getId());
    }

    /**
     * 查看住房类型列表
     */
    @Override
    public PageResponse<MainDataHousingTypeListDTO> list(MainDataHousingTypeListReq req) {
        Page<MainDataHousingType> page = new Page<>(req.getPageNo(), req.getPageSize());
        QueryWrapper<MainDataHousingType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(null != req.getStatus(),MainDataHousingType::getStatus,req.getStatus());
        queryWrapper.lambda().eq(MainDataHousingType::getIsDeleted,IsDeletedCode.NO.getCode());
        if(StringUtils.isNotBlank(req.getName())){
            queryWrapper.lambda().like(MainDataHousingType::getTypeCode,req.getName()).
                    or().like(MainDataHousingType::getTypeName,req.getName());
        }
        //需定义排序规则:按修改时间倒序
        queryWrapper.lambda().orderByDesc(MainDataHousingType::getGmtModified);
        Page<MainDataHousingType> iPage = baseMapper.selectPage(page, queryWrapper);
        List<MainDataHousingType> mainDataHousingTypeList = iPage.getRecords();
        if(CollectionUtils.isEmpty(mainDataHousingTypeList)){
            return new PageResponse<>(new ArrayList<>(),iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
        }
        List<MainDataHousingTypeListDTO> mainDataHousingTypeListDTOS = EntityUtil.copyList(mainDataHousingTypeList, MainDataHousingTypeListDTO.class);
        return new PageResponse<>(mainDataHousingTypeListDTOS, iPage.getCurrent() == 1, iPage.getTotal() <= (iPage.getCurrent() * iPage.getSize()), iPage.getTotal(), iPage.getCurrent(), iPage.getSize());
    }

    /**
     * 查看住房类型
     */
    @Override
    public Result<MainDataHousingTypeQueryDTO> query(MainDataHousingTypeUpdateStatusReq req) {
        QueryWrapper<MainDataHousingType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("id",req.getId());
        queryWrapper.eq("is_deleted",IsDeletedCode.NO.getCode());
        MainDataHousingType mainDataHousingType = mainDataHousingTypeMapper.selectOne(queryWrapper);
        MainDataHousingTypeQueryDTO mainDataHousingTypeQueryDTO = EntityUtil.copy(mainDataHousingType, MainDataHousingTypeQueryDTO.class);
        return Result.success(mainDataHousingTypeQueryDTO);
    }
}
